// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.

package com.smslib.util;

import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class SendMessage
{
	public void doIt() throws Exception
	{
		OutboundNotification outboundNotification = new OutboundNotification();
		System.out.println("发送代码示例：");
		System.out.println(Library.getLibraryDescription());
		System.out.println("Version: " + Library.getLibraryVersion());
		SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM4", 9600, "Wavecom", "");		//发送成功的端口号
		gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("0000");
		// Explicit SMSC address set is required for some modems.
		// Below is for VODAFONE GREECE - be sure to set your own!
//		gateway.setSmscNumber("+306942190000");		//设置短信服务中心号码，这句话注释后短信发送成功，考虑sim卡中已经保存过短信服务中心号码，再次设置即为错误。
		Service.getInstance().setOutboundMessageNotification(outboundNotification);
		Service.getInstance().addGateway(gateway);
		Service.getInstance().startService();
		System.out.println("服务启动成功");
		System.out.println();
		System.out.println("Modem Information:");
		System.out.println("  Manufacturer: " + gateway.getManufacturer());
		System.out.println("  Model: " + gateway.getModel());
		System.out.println("  Serial No: " + gateway.getSerialNo());				//序列号信息
		System.out.println("  SIM IMSI: " + gateway.getImsi());						
		System.out.println("  Signal Level: " + gateway.getSignalLevel() + " dBm");	//信号强度
		System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");	//电池电量信息 考虑注销  硬件问题，没有具体信息
																					//读取sim卡信息！！！ 应该获得更多信息 基本信息 供数据库存储使用 卡里的信息！
		System.out.println();
		// Send a message synchronously.
		OutboundMessage msg = new OutboundMessage("+8613460307985", "Hello from SMSLib!~from anwei's message cat!");	//绅绅号码！ 2016-11-7 23:42:34测试成功
		Service.getInstance().sendMessage(msg);
		System.out.println(msg);
		System.out.println("Now Sleeping - Hit <enter> to terminate.");
		System.in.read();
		Service.getInstance().stopService();
	}

	public class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(AGateway gateway, OutboundMessage msg)
		{
			System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());
			System.out.println(msg);
		}
	}

	public static void main(String args[])
	{
		SendMessage app = new SendMessage();
		try
		{
			app.doIt();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
